package com.hmall.common.config;

import org.springframework.amqp.core.*;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnProperty(value = "spring.rabbitmq.listener.simple.retry.enabled", havingValue = "true")
public class MqConsumeErrorAutoConfiguration {

    @Value("${spring.application.name}")
    private String SERVICE_NAME;

    @Bean
    public DirectExchange errorDirectExchange() {
        return ExchangeBuilder
                .directExchange("error.direct")
                .build();
    }

    @Bean
    public Queue serviceErrorQueue() {
        return  QueueBuilder
                .durable(SERVICE_NAME + ".error.queue")
                .build();
    }

    @Bean
    public Binding serviceBinding() {
        return BindingBuilder
                .bind(serviceErrorQueue())
                .to(errorDirectExchange())
                .with(SERVICE_NAME);
    }
}